<?php
/* ----------------------------------------------------------
--               JacoCMS by Jaco Ruit                      --
-------------------------------------------------------------
--     JacoCMS is Open Source and is lincenced under       --
--             GNU General Public Licence v3               --
--          http://www.gnu.org/copyleft/gpl.html           --
-------------------------------------------------------------
--       Please don't remove any text from the footers.    --
-------------------------------------------------------------
-- Credits:   * Jaco Ruit          ~     Scripts,MySQL     --     
-----------------------------------------------------------*/

#important includes
include '../includes/config.php';
include '../includes/database.ext.class.php'; 

#create database connection
$database = new Database();
$database->connect('../includes/config.php');

#finish includes
include '../includes/core.ext.class.php';
include '../includes/settings.class.php';
include '../includes/user.class.php';

#vars, classes
$settings = new Settings();
$core = new Core();
$user = new User();
$message = "";


session_start();
if((isset($_SESSION['jcmsuname'])) && (isset($_SESSION['jcmspwd']))) //if((isset($_SESSION['jcmsuname'])) && (isset($_SESSION['jcmspwd'])))
{
	$uname = $core->filter($_SESSION['jcmsuname']);
	$pwd = $core->filter($_SESSION['jcmspwd']);
	
	#validate cookies' info
	if ($user->validateCookie($uname, $pwd) == true)
	{
		header("Location: panel");
	}
	else
	{
		session_destroy();


	}
}

#check for msgcode
if(isset($_GET['msg']))
{
	$msg = $core->filter($_GET['msg']);
	if($msg == '1')
	{
		$message = '<div id="msg"><p>Your account registration is completed.<br /> You may login now.</p></div>';
	}
	else if ($msg == '2')
	{
		$message = '<div id="error"><p>Wrong username/password combination!<br /> Please, try again.</p></div>';
	}
	else if ($msg == '3')
	{
		$message = '<div id="msg"><p>You have been successfully logged out</p></div>';
	}
	else
	{
		$msg = null;
		$message = "";
	}
}

$formcode = '<h3>Login to ' . $settings->getWebsiteName() . '</h3>
			<div id="form">
			<form name="loginform" action="auth.php" method="post">
			<p>Username</p> <input type="text" name="uname" size="35" /><br />
			<p>Password</p> <input type="password" name="pwd" size="35"/><br />
			<input type="submit" value="Login" /></form>
			<a href="register">Register new account</a>
			</div>';

#create template
$smarty->assign("page", "Login");
$smarty->assign("msg", $message);
$smarty->assign("webname", $settings->getWebsiteName());
$smarty->assign("formcode", $formcode);
$smarty->display("login.tpl");

?>